package vng.wcc.sonph.LauncherGenerator;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

public class TabRowView extends LinearLayout{

	private TabFormatter tabformatter;
	private LauncherView launcherView;

	/**
	 * TabRowView is a tab lying on the bottom of screen, it is used to navigate between pages 
	 * @param context
	 * @param tabData contains information about the items to be displayed on the tab (icon,highlight icon)
	 * @param launcherView contains the launcher screens
	 * @param formatter
	 */
	public TabRowView(Context context,final TabCellInfo[] tabData,Formatter formatter) {
		super(context);
		
		this.tabformatter = new TabFormatter(formatter, tabData.length);
		
		addChildren(context, tabData);
	}
	
	public void attachToLauncher(LauncherView launcher) {
		this.launcherView = launcher;
	}

	/**
	 * Add tab cell into the row
	 * @param context
	 * @param tabData contains information about the items to be displayed on the tab (icon,highlight icon)
	 */
	private void addChildren(Context context, final TabCellInfo[] tabData) {
		for(int i = 0 ; i < tabData.length ; i++ ) {
			TabCellView cell = createTableCell(context,tabData[i]);			
			cell.setOnClickListener(new OnTabItemClickListener(i));			
			this.addView(cell);
		}
		
		TabCellView firstCell = (TabCellView) this.getChildAt(0);
		firstCell.displayHighlightIcon();
	}


	/**
	 * Create cell base on data provided
	 * @param context
	 * @param info contains information about the item to be displayed on the tab (icon,highlight icon)
	 * @return
	 */
	private TabCellView createTableCell(Context context,TabCellInfo info) {
		
		TabCellView cell = new TabCellView(context,info.getIconBitmap(),info.getActiveIconBitmap());
		
		cell.setPadding(
				tabformatter.getTabLeftPadding(),
				tabformatter.getTabTopPadding(),
				tabformatter.getTabRightPadding(),
				tabformatter.getTabBottomPadding());
		
		//Be default, all icon is "normal" (not highlight)
		cell.displayNormalIcon();		
		return cell;
	}
	
	public void changeHighLight(int currentId,int targetId) {
		unsetHighLight(currentId);
		setHighLight(targetId);
	}
	
	public void unsetHighLight(int cellId) {
		TabCellView cell = (TabCellView) this.getChildAt(cellId);
		cell.displayNormalIcon();
	}
	
	public void setHighLight(int cellId) {
		TabCellView cell = (TabCellView) this.getChildAt(cellId);
		cell.displayHighlightIcon();
	}

	
	private class OnTabItemClickListener implements OnClickListener {
		
		private int targetId;
		public OnTabItemClickListener(int targetId) {
			this.targetId = targetId; 
		}
		
		/**
		 * On click handler: when user clicks on a tab item
		 * the screen displays the accordingly page 
		 */
		@Override
		public void onClick(View v) {	
			final int currentId = launcherView.getCurrentScreenId();
			
			//nothing to do
			if ( currentId == targetId ) return;
			
			//change the highlight icon - and display the new screen
			TabRowView.this.changeHighLight(currentId, targetId);		
			launcherView.setToScreen(targetId);					
		}
				
	}
}
